package io.jft.doll.model.viewModel

import android.arch.lifecycle.MutableLiveData
import com.g.base.extend.switchMap
import com.g.base.viewModel.BaseViewModel
import com.google.gson.JsonObject
import io.jft.doll.room.repository.DollRepository

/**
 * Created by G on 2017/12/5 0005.
 */
class DollViewModel : BaseViewModel() {
    private val dollRepository by lazy { DollRepository() }
    private val dollListOperate by lazy { MutableLiveData<JsonObject>() }
    val dollLimit = 20

    /**
     * 操作一下娃娃列表
     */
    fun operateDollList(offset: Int, id: String = "") {
        dollListOperate.value =
                JsonObject()
                        .apply {
                            addProperty("offset", offset.toString())
                            addProperty("limit", dollLimit.toString())
                            if (id.isNotEmpty())
                                addProperty("id", id)
                        }
    }

    /**
     * 观察娃娃列表
     */
    fun obsDollList() = dollListOperate.switchMap {
        if (it.get("id")?.asString?.isNotEmpty() == true) {
            dollRepository.getOtherOwnerDoll(it)
        } else {
            dollRepository.getSelfOwnerDoll(it)
        }
    }

    /**
     * 获取娃娃详情
     */
    fun getDollDetail(id: String) = dollRepository.getDollDetail(id)

    /**
     * 兑换娃娃币
     */
    fun dollToCoin(id: String) = dollRepository.dollToCoin(id)
}